package com.cuit.bysj.service;

import java.util.ArrayList;
import java.util.List;

import com.cuit.bysj.dao.TcherDao;
import com.cuit.bysj.entities.Appraise;
import com.cuit.bysj.entities.Student;
import com.cuit.bysj.entities.TcherResults;
import com.cuit.bysj.entities.Teacher;

public class TcherService {
	private TcherDao tcherDao;

	public TcherDao getTcherDao() {
		return tcherDao;
	}

	public void settcherDao(TcherDao tcherDao) {
		this.tcherDao = tcherDao;
	}

	// 查询返回List
	@SuppressWarnings("rawtypes")
	public List tcherlist() {
		String hql = "from Teacher";

		List list = tcherDao.querylistByhql(hql);

		if (list.size() > 0) {
			return list;
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Appraise> findListbyTcherId(String id, String cname, String banji) {
		String hql = "from Appraise a where a.teacher = " + id
				+ " and a.score > 0 and a.content <> null and a.subject.sName=" + "'" + cname + "'"
				+ " and a.student.banji=" + "'" + banji + "'";

		List<Appraise> list = tcherDao.querylistByhql(hql);

		return list;
	}

	// 根据老师查询老师所教班级列表
	public List<TcherResults> initTcherSeelist(Integer id) {
		String hql = "select a.student.major,a.student.banji,a.subject.sName,avg(a.score) from Appraise a where a.teacher="
				+ id + "group by a.student.major,a.student.banji";

		List list = tcherDao.querylistByhql(hql);
		List<TcherResults> results = new ArrayList<TcherResults>();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			TcherResults a = new TcherResults(String.valueOf(obj[0]), String.valueOf(obj[1]), String.valueOf(obj[2]),
					String.valueOf(obj[3]));
			results.add(a);
		}
		return results;
	}
	
	public Teacher findTcher(Integer id) {
		return tcherDao.getByid(id);
	}

	public void updateTcher(Teacher t) {
		tcherDao.updateTcher(t);
	}
}
